module;

#include <boost/asio.hpp>
#include <boost/mysql.hpp>

export module MySQL_connect;
import std;
import MySQL_config;
using namespace boost;

export namespace MySQL_connect {
	asio::io_context ioc;
	mysql::any_connection conn(ioc);

	void connect(std::ostream& log = std::clog, std::ostream& err = std::cerr) {
		mysql::connect_params params;
		params.server_address.emplace_host_and_port(MySQL_config::serverIp);
		params.username = MySQL_config::userName;
		params.password = MySQL_config::password;
		params.database = MySQL_config::databaseName;

		// Connect to the server
		log << "Connecting to MySQL server...\n";
		try {
			conn.connect(params);
		}
		catch (std::exception& e) {
			err << std::format("Error connecting to MySQL server: {}\n", e.what());
			return;
		}
		log << "Successfully connected to MySQL server.\n";
	}
}